package com.woniuxy.consumer.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.api.SearchApi;
import com.woniuxy.api.StuApi;
import com.woniuxy.common.model.dto.PageDto;
import com.woniuxy.common.model.dto.PageRequest;
import com.woniuxy.common.result.Result;
import com.woniuxy.provider.model.entity.Stu;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/consumer")
@Slf4j
public class SchoolController {
    @Autowired
    private StuApi stuApi;
    @Autowired
    private SearchApi searchApi;

    @PostMapping("/list")
    @ApiOperation("分页查询全部")
    public Result<Page<Stu>> list(@RequestBody PageRequest pageRequest) {
        Result<Page<Stu>> result = stuApi.list(pageRequest);
        log.info("我是服务消费者 ===》 进行了调远程调用");
        return result;
    }

    @PostMapping("/remove")
    @ApiOperation("删除")
    public Result remove(@RequestParam("id") Integer id) {
        stuApi.remove(id);
        System.out.println("id:"+ id);
        return Result.ok("删除成功，已发送删除消息至 RabbitMQ！");
    }

    @PostMapping("/save")
    @ApiOperation("批量保存到 Elasticsearch")
    public Result<String> save(){
        searchApi.save();
        return Result.ok("保存成功！");
    }

    @PostMapping("/search")
    @ApiOperation("通过 info 字段分页搜索学生")
    public Result<Page<Stu>> search(@RequestBody PageDto pageDto) {
        Result<Page<Stu>> result = searchApi.search(pageDto);
        return result;
    }


}
